package com.hc.controller;

import com.alibaba.fastjson.JSON;
import com.hc.bean.AdminInfo;
import com.hc.bean.Functions;
import com.hc.bean.GroupList;
import com.hc.bean.TreeNode;
import com.hc.service.AdminService;
import com.hc.service.impl.FunctionServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/adminInfo")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private FunctionServiceImpl functionService;

    /*返回JSON字符串需要加produces*/
    @RequestMapping(value = "/login",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String adminLogin(AdminInfo adminInfo, HttpSession session){
//        System.out.println("收到登录请求");
//        System.out.println(adminInfo);
        Map result=new HashMap();
        if(adminService.isAdmin(adminInfo.getName())){
            AdminInfo admin=adminService.adminLogin(adminInfo);
//            System.out.println("查到了admin"+admin);
            if(admin!=null){
                result.put("success","true");
                result.put("message","登录成功");
                session.setAttribute("admin",admin);
            }else {
                result.put("success","false");
                result.put("message","密码错误");
            }
        }else {
            result.put("success","false");
            result.put("message","非管理员账号");
        }
        String jsonStr= JSON.toJSONString(result);
        return jsonStr;
    }

    @RequestMapping("/getTree")
    @ResponseBody
    public List<TreeNode> getTree(Integer adminId){
        List<TreeNode> treeNodes = functionService.treeNodeList(adminId);
//        System.out.println(treeNodes);
        return treeNodes;
    }

    @RequestMapping("/logout")
    @ResponseBody
    public Map<String,Object> logout(HttpSession session){
        session.invalidate();
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("success","true");
        return resultMap;
    }

    @RequestMapping("/getAdmin")
    @ResponseBody
    public List<AdminInfo> getAdmin(){
        List<AdminInfo> adminInfoList=adminService.getAdminInfo();
        AdminInfo adminInfo=new AdminInfo();
        adminInfo.setId(0);
        adminInfo.setName("请选择管理员");
        adminInfoList.add(0,adminInfo);
        return adminInfoList;
    }

    @RequestMapping("/getGroupList")
    @ResponseBody
    public List<GroupList> getGroupList(Integer adminId){
        List<TreeNode> functions=functionService.treeNodeList(adminId);
        List<GroupList> groupListList=new ArrayList<>();
        for (TreeNode function : functions) {
            GroupList groupList=new GroupList();
        }
        return groupListList;
    }
}
